package com.jucai.pppeiyouclient.view.activity;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import com.google.gson.Gson;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.SubConversationListAdapterEx;
import com.jucai.pppeiyouclient.base.BaseApplication;
import com.jucai.pppeiyouclient.bean.IMUserBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.LoginUtils;
import com.jucai.pppeiyouclient.utils.StringUtil;

import java.util.HashMap;
import java.util.Map;

import io.rong.imkit.RongContext;
import io.rong.imkit.RongIM;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.UserInfo;

/**
 * Created by Kuky on 2017-05-06.
 */

public class ConversationListActivity extends FragmentActivity implements View.OnClickListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.conversationlist);
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_title_text).setVisibility(View.INVISIBLE);
        findViewById(R.id.common_home_back).setVisibility(View.INVISIBLE);

        Fragment fragment = getConversationList();
        getSupportFragmentManager().beginTransaction().add(R.id.conversatonLl, fragment).commit();
        initData();
    }

    private void initData() {
        RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() {

            @Override
            public UserInfo getUserInfo(String userId) {
                if (!StringUtil.isChinese(userId))
                    getFriendUserInfo(userId);
                return null;//根据 userId 去你的用户系统里查询对应的用户信息返回给融云 SDK。
            }

        }, true);

        RongIM.getInstance().setCurrentUserInfo(new UserInfo(LoginUtils.getRongyun_id(ConversationListActivity.this), LoginUtils.getMember_nickname(ConversationListActivity.this), Uri.parse(LoginUtils.getMember_avatar(ConversationListActivity.this))));
    }

    private void getFriendUserInfo(String userId) {
        Map<String, String> mapWallet = new HashMap<>();
        mapWallet.put("service", "Public_Chat.getMemberChatData");
        mapWallet.put("target_member_id", userId);

        CommonRequestNetData.getJsonStringDataByEncrpt(ConversationListActivity.this, mapWallet, new CommonRequestNetData.ResultCallBack() {
            @Override
            public void parseJsonString(String str) {
                try {

                    IMUserBean dataBean = new Gson().fromJson(str, IMUserBean.class);
                    RongIM.getInstance().refreshUserInfoCache(new UserInfo(dataBean.rongyun_id, dataBean.rongyun_nickname, Uri.parse(dataBean.rongyun_avatar)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String str) {
                LogUtils.e("jsonObject：" + str);
            }


        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;
        }
    }

    private Fragment getConversationList() {
        ConversationListFragment listFragment = new ConversationListFragment();
        listFragment.setAdapter(new SubConversationListAdapterEx(RongContext.getInstance()));
        Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false")
                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")
                .appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")
                .appendQueryParameter(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(), "false")
                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")
                .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "true")
                .build();
        Conversation.ConversationType[] mConversationsTypes = new Conversation.ConversationType[]{Conversation.ConversationType.PRIVATE,
                Conversation.ConversationType.GROUP,
                Conversation.ConversationType.PUBLIC_SERVICE,
                Conversation.ConversationType.APP_PUBLIC_SERVICE,
                Conversation.ConversationType.SYSTEM,
                Conversation.ConversationType.DISCUSSION
        };
        listFragment.setUri(uri);
        return listFragment;
    }
}
